<template>

  <div style="background-color: #fff;">
    <newPageHeader></newPageHeader>
    <div class="supplier-register">
      <el-row>
        <el-col :span="8">
          <div class="left-section">
            <div class="leftTitle">
              <h2 class="title">
                实力供应商
              </h2>
              <div class="recommend">推荐</div>
            </div>
            <p class="description">汇聚海量商品，多品类覆盖丰富客群，真实认证服务，让买家快速了解企业实力
            </p>
            <ul class="steps">
              <li>1、请填写真实资料进行注册</li>
              <li>2、注册提交后等待管理员审核</li>
              <li>3、审核通过后即可登录</li>
              <li>
                4、会员缴费完成入驻，即可开设店铺，上传产品及公司资料</li>
            </ul>
          </div>
        </el-col>
        <el-col :span="14">
          <div class="right-section">
            <h2 class="titleTwo">卖家申请入驻</h2>
            <el-form :model="form" :rules="rules" ref="form" label-width="140px">
              <el-form-item label="公司名称：" prop="company_title">
                <el-input class="inPutStype" v-model="form.company_title" placeholder="请输入营业执照上公司名称"></el-input>
              </el-form-item>
              <el-form-item label="联系人姓名：" prop="name">
                <el-input class="inPutStype" v-model="form.name" placeholder="请输入联系人姓名"></el-input>
              </el-form-item>
              <el-form-item label="手机号码：" prop="mobile">
                <el-input class="inPutStype" v-model="form.mobile" placeholder="请输入11位联系手机号码"></el-input>
              </el-form-item>
              <el-form-item label="验证码：" prop="code">
                <el-input class="inPutStype" v-model="form.code" placeholder="请输入">
                </el-input>
                <div class="getCode" @click="getVerificationCode" v-if="timeNum < 1">获取验证码</div>
                <div class="getCode" v-if="timeNum > 0">{{ timeNum }}</div>
              </el-form-item>
              <el-form-item label="联系电话：" prop="contact_phone">
                <el-input class="inPutStype" v-model="form.contact_phone" placeholder="请输入联系电话"></el-input>
              </el-form-item>
              <el-form-item label="邀请码：" prop="invite">
                <el-input type="password" class="inPutStype" v-model="form.invite" placeholder="请输入邀请码"></el-input>
              </el-form-item>
              <el-form-item label="登录密码：" prop="pwd">
                <el-input show-password class="inPutStype" type="password" v-model="form.pwd"
                  placeholder="请输入登录密码"></el-input>
              </el-form-item>
              <el-form-item label="确认密码：" prop="pwd2">
                <el-input show-password class="inPutStype" type="password" v-model="form.pwd2"
                  placeholder="再次输入密码"></el-input>
              </el-form-item>
              <el-form-item label="资质信息：" prop="license_img">
                <el-upload action="/api/upload" :limit="5" :show-file-list="true" :on-success="handleSuccesslicenseimg"
                  :before-upload="beforeUpload" :on-remove="handleRemovelicenseimg" class="upload-box">
                  <img src="../../../static/engineering5188/supplier/upIcon.png" alt="">
                  <div class="el-upload__text">请上传资质</div>
                </el-upload>
                <div class="file-tips">文件大小不超过10MB；格式支持：jpg、png、jpeg、pdf</div>
              </el-form-item>
              <el-form-item label="法人身份证信息：" prop="legal_img">
                <el-upload action="/api/upload" :limit="5" :on-success="handleSuccesslegalimg"
                  :before-upload="beforeUpload" :on-remove="handleRemovelegalimg" class="upload-box">
                  <img src="../../../static/engineering5188/supplier/upIcon.png" alt="">
                  <div class="el-upload__text">请上传法人身份证</div>
                </el-upload>
                <div class="file-tips">文件大小不超过10MB；格式支持：jpg、png、jpeg、pdf</div>
              </el-form-item>
              <el-form-item label="其它证件：" prop="other_img">
                <el-upload action="/api/upload" :limit="5" :show-file-list="true" :on-success="handleSuccessotherimg"
                  :before-upload="beforeUpload" :on-remove="handleRemoveotherimg" class="upload-box">
                  <img src="../../../static/engineering5188/supplier/upIcon.png" alt="">
                  <div class="el-upload__text">请上传其它证件</div>
                </el-upload>
                <div class="file-tips">文件大小不超过10MB；格式支持：jpg、png、jpeg、pdf</div>
              </el-form-item>
              <el-form-item>
                <el-button class="buttonClose" type="danger" @click="resetForm('form')">取消</el-button>
                <el-button class="buttonPay" type="primary" @click="submitForm('form')">提交申请</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-col>
      </el-row>
    </div>
    <newPageFooter></newPageFooter>
  </div>
</template>
<script>
import { API_ROOT } from '../../../config/env';
import { mapState } from 'vuex';
import newPageHeader from '../../../components/page/engineering5188/new-page-header.vue';
import newPageFooter from '../../../components/page/engineering5188/new-page-footer.vue';
export default {
  name: "applyForDdmission",
  components: {
    newPageHeader,
    newPageFooter
  },
  data() {
    return {
      tokenData: "",
      form: {
        company_title: '',
        name: '',
        contact_phone: '',
        mobile: '',
        code: '',
        invite: '',
        pwd: '',
        pwd2: '',
        license_img: [],
        legal_img: [],
        other_img: []
      },
      rules: {
        company_title: [{ required: true, message: '请输入公司名称', trigger: 'blur' }],
        name: [{ required: true, message: '请输入联系人姓名', trigger: 'blur' }],
        mobile: [{ required: true, message: '请输入手机号码', trigger: 'blur' }, { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }],
        pwd: [{ required: true, message: '请输入登录密码', trigger: 'blur' }],
        pwd2: [{ required: true, message: '请确认密码', trigger: 'blur' }, { validator: this.confirmPasswordValidator, trigger: 'blur' }],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
        license_img: [{ required: true, message: '请上传资质', trigger: 'blur' }],
        legal_img: [{ required: true, message: '请上传法人身份证', trigger: 'blur' }],
        other_img: [{ required: true, message: '请上传其它证件', trigger: 'blur' }],
      },
      timeNum: 0,
    };
  },
  computed: {
    ...mapState([
      "userInfo",
    ]),
  },
  created() {
    this.tokenData = this.userInfo.token;
    console.log(this.tokenData, this.API_ROOT);
  },
  methods: {

    getVerificationCode() {
      // 这里添加获取验证码的逻辑，比如调用后端接口
      if (this.form.mobile) {
        this.$httpApi('/api/send', {
          mobile: this.form.mobile
        }, 'post').then(res => {
          if (res.code == 200) {
            console.log(res);
            this.timeNum = 60;
            setInterval(() => {
              this.timeNum--;
              if (this.timeNum <= 0) {
                clearInterval();
              }
            }, 1000)

          } else {
            alertErr(res.msg);
          }
        })
      } else {
        alertErr('请输入手机号码');
      }

      console.log('获取验证码');
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 这里添加提交表单的逻辑，比如调用后端接口
          console.log('提交表单');

          this.$httpApi("/api/supAuth", {
            company_title: this.form.company_title,
            name: this.form.name,
            contact_phone: this.form.contact_phone,
            mobile: this.form.mobile,
            code: this.form.code,
            invite: this.form.invite,
            pwd: this.form.pwd,
            pwd2: this.form.pwd2,
            license_img: this.form.license_img.join(','),
            legal_img: this.form.legal_img.join(','),
            other_img: this.form.other_img.join(',')
          }, "post").then(res => {
            if (res.code == 200) {
              this.$router.push('/Successful')
            } else {
              alertErr(res.msg);
            }
          })
        } else {
          console.log('表单验证不通过');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

    handleSuccesslicenseimg(response, file, fileList) {
      file.file_url = response.data.path;
      this.form.license_img.push(response.data.path)
    },
    handleRemovelicenseimg(response, file, fileList) {
      let temp = [];
      for (let i = 0; i < this.form.license_img.length; i++) {
        if (this.form.license_img[i] != file.file_url) {
          temp.push(this.form.license_img[i])
        }
      }
      this.form.license_img = temp;
    },
    handleSuccesslegalimg(response, file, fileList) {
      file.file_url = response.data.path;
      this.form.legal_img.push(response.data.path)
    },
    handleRemovelegalimg(response, file, fileList) {
      let temp = [];
      for (let i = 0; i < this.form.legal_img.length; i++) {
        if (this.form.legal_img[i] != file.file_url) {
          temp.push(this.form.legal_img[i])
        }
      }
      this.form.legal_img = temp;
    },
    handleSuccessotherimg(response, file, fileList) {
      file.file_url = response.data.path;
      this.form.other_img.push(response.data.path)
    },
    handleRemoveotherimg(response, file, fileList) {
      let temp = [];
      for (let i = 0; i < this.form.other_img.length; i++) {
        if (this.form.other_img[i] != file.file_url) {
          temp.push(this.form.other_img[i])
        }
      }
      this.form.other_img = temp;
    },
    beforeUpload(file) {
      // 上传前的处理逻辑，比如文件大小限制等
      const isLt2M = file.size / 1024 / 1024 < 10;
      if (!isLt2M) {
        this.$message.error('文件大小不能超过10MB!');
      }
      return isLt2M;
    },
    confirmPasswordValidator(rule, value, callback) {
      if (value !== this.form.pwd) {
        callback(new Error('两次输入的密码不一致!'));
      } else {
        callback();
      }
    }
  }
};
</script>

<style scoped lang="less">
/deep/ .el-form-item__error {
  left: 130px;
}

/deep/ .el-form-item__label {
  width: 125px;
}

.right-section {
  width: 800px;

}

.getCode {
  position: absolute;
  right: 50px;
  top: 0px;
  padding: 0 15px;
  background-color: #fff;
  bottom: 0;
  margin: auto;
  height: 30px;
  font-family: PingFang SC, PingFang SC;
  font-weight: 400;
  font-size: 14px;
  color: #0052D9;
  line-height: 30px;
  font-style: normal;
  text-transform: none;
  cursor: pointer;
}

/deep/ .el-upload {
  border: 1px solid #EBEBEB;
  width: 150px;
  height: 150px;
  font-family: PingFang SC, PingFang SC;
  font-weight: 400;
  font-size: 14px;
  color: #A2A2A2;

  text-align: center;
  font-style: normal;
  text-transform: none;
}

.upload-container {
  padding: 20px;
}

.upload-box img {
  width: 32px;
  height: 24px;
  margin-top: 50px;
}

.upload-box {
  //   border: 1px dashed #ccc;
  border-radius: 4px;
  //   text-align: center;
  //   padding: 70px 0;
  cursor: pointer;
}

.file-tips {

  margin-top: 5px;
  text-align: left;
  padding-left: 125px;

  font-family: PingFang SC, PingFang SC;
  font-weight: 400;
  font-size: 12px;
  color: #999999;

  font-style: normal;
  text-transform: none;
}

// /deep/ .el-form-item__content{
//   margin-left:0 !important;
// }
.buttonClose {
  width: 160px;
  height: 44px;
  background: #F5F5F5;
  border-radius: 4px 4px 4px 4px;
  border: 1px solid #D7D7D7;
  font-family: PingFang SC, PingFang SC;
  font-weight: 400;
  font-size: 14px;
  color: #666666;
  line-height: 18px;
  text-align: center;
  font-style: normal;
  text-transform: none;
  margin-top: 48px;
}

.buttonPay {
  width: 160px;
  height: 44px;
  background: #014BC4;
  box-shadow: 0px 3px 6px 1px rgba(0, 0, 0, 0.05);
  border-radius: 4px 4px 4px 4px;
  font-family: PingFang SC, PingFang SC;
  font-weight: 400;
  font-size: 14px;
  color: #FFFFFF;

  text-align: center;
  font-style: normal;
  text-transform: none;
  margin-left: 16px;
}

.inPutStype {
  width: 630px;
}

.supplier-register {
  padding: 0 0 0 260px;
  margin-bottom: 79px;
}

.leftTitle {
  display: flex;
  align-items: center;
  margin-top: 160px;
}

.left-section {
  padding-right: 20px;
}

.titleTwo {
  font-size: 40px;
  font-weight: 600;
  margin-bottom: 19px;
  margin-top: 66px;
  padding-left: 200px;
  font-size: 36px;
  margin-bottom: 20px;
}

.title {
  font-size: 40px;
  font-weight: 600;
  margin-bottom: 19px;
}

.recommend {
  width: 77px;
  height: 40px;
  background: #ED232A;
  border-radius: 4px 4px 4px 4px;
  font-family: PingFang SC, PingFang SC;
  font-weight: 500;
  font-size: 26px;
  color: #FFFFFF;
  line-height: 40px;
  text-align: center;
  font-style: normal;
  text-transform: none;
  margin-left: 14px;
  margin-bottom: 14px;
}

.description {
  width: 482px;
  height: 56px;
  font-family: PingFang SC, PingFang SC;
  font-weight: 600;
  font-size: 20px;
  color: #0F0F0F;
  line-height: 28px;
  text-align: left;
  font-style: normal;
  text-transform: none;
  margin-bottom: 60px;
}

.steps {
  width: 375px;
  height: 140px;

}

.steps li {
  font-family: PingFang SC, PingFang SC;
  font-weight: 400;
  font-size: 14px;
  color: #000000;
  line-height: 40px;
  text-align: left;
  font-style: normal;
  text-transform: none;
  //   list-style: decimal;
  //   margin-left: 20px;
  //   margin-bottom: 5px;
}
</style>